/**
 * 
 */
package net.sourceforge.ftgl.util;

import javax.media.opengl.GL;

import net.sourceforge.ftgl.math.FTBBox;
import net.sourceforge.ftgl.math.Vector3f;

/**
 * @author funsheep
 *
 */
public class RenderUtils
{
	
	
	/**
	 * Renders the given FTBBox at position <code>pos</code> onto the screen using gl.
	 * @param box The box to render.
	 * @param pos The position, where to render.
	 * @param gl The gl context object.
	 * @return Returns always <code>true</code>, for use with <code>asserts</code>.
	 */
	public static final boolean renderFTBBox(FTBBox box, Vector3f pos, GL gl)
	{
		gl.glPushMatrix();
		gl.glTranslated(pos.x, pos.y, pos.z);
		gl.glBitmap(0, 0, 0f, 0f, pos.x, pos.y, null, 0);
		//TODO: Move to correct raster position
		gl.glPushAttrib(GL.GL_CURRENT_BIT | GL.GL_LIGHTING_BIT | GL.GL_COLOR_BUFFER_BIT);
		gl.glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
		gl.glDisable(GL.GL_LIGHTING);
		gl.glDisable(GL.GL_BLEND);
		// Draw the front face
		gl.glBegin(GL.GL_LINE_LOOP);
		gl.glVertex3f(box.lowerX, box.lowerY, box.lowerZ);
		gl.glVertex3f(box.lowerX, box.upperY, box.lowerZ);
		gl.glVertex3f(box.upperX, box.upperY, box.lowerZ);
		gl.glVertex3f(box.upperX, box.lowerY, box.lowerZ);
		gl.glEnd();
		// Draw the back face
		if (box.lowerZ != box.upperZ)
		{
			gl.glBegin(GL.GL_LINE_LOOP);
			gl.glVertex3f(box.lowerX, box.lowerY, box.upperZ);
			gl.glVertex3f(box.lowerX, box.upperY, box.upperZ);
			gl.glVertex3f(box.upperX, box.upperY, box.upperZ);
			gl.glVertex3f(box.upperX, box.lowerY, box.upperZ);
			gl.glEnd();
			// Join the faces
			gl.glBegin(GL.GL_LINES);
			gl.glVertex3f(box.lowerX, box.lowerY, box.lowerZ);
			gl.glVertex3f(box.lowerX, box.lowerY, box.upperZ);

			gl.glVertex3f(box.lowerX, box.upperY, box.lowerZ);
			gl.glVertex3f(box.lowerX, box.upperY, box.upperZ);

			gl.glVertex3f(box.upperX, box.upperY, box.lowerZ);
			gl.glVertex3f(box.upperX, box.upperY, box.upperZ);

			gl.glVertex3f(box.upperX, box.lowerY, box.lowerZ);
			gl.glVertex3f(box.upperX, box.lowerY, box.upperZ);
			gl.glEnd();
		}
		gl.glPopAttrib();
		gl.glPopMatrix();
		gl.glBitmap(0, 0, 0f, 0f, -pos.x, -pos.y, null, 0);
		return true;
	}

	
	/**
	 * Render to texture.
	 * @param index index of the texture
	 * @return always <code>true</code>
	 */
	public static final boolean renderTexture(int textureID, int width, int height, GL gl)
	{
		if (textureID >= 0)
		{
			gl.glPushAttrib(GL.GL_ENABLE_BIT | GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_CURRENT_BIT);

			gl.glEnable(GL.GL_BLEND);
			gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); // GL_ONE
			gl.glDepthFunc(GL.GL_ALWAYS);
			
			gl.glBindTexture(GL.GL_TEXTURE_2D, textureID);
			
			float sizeX = Math.min(256f, width);
			float sizeY = (sizeX/width)*height;
			sizeX /= 2f;
			sizeY /= 2f;
			gl.glBegin(GL.GL_QUADS);
			gl.glColor4f(1f,0f,0f,1f);
			gl.glVertex2f(-sizeX, -sizeY);
			gl.glVertex2f(-sizeX, sizeY);
			gl.glVertex2f(sizeX, sizeY);
			gl.glVertex2f(sizeX, -sizeY);
			gl.glEnd();
			gl.glBegin(GL.GL_QUADS);
			gl.glTexCoord2f( 0f, 0f);
			gl.glVertex2f(-sizeX, -sizeY);

			gl.glTexCoord2f(0f, 1f);
			gl.glVertex2f(-sizeX, sizeY);

			gl.glTexCoord2f(1f, 1f);
			gl.glVertex2f(sizeX, sizeY);

			gl.glTexCoord2f(1f, 0f);
			gl.glVertex2f(sizeX, -sizeY);
			gl.glEnd();
			gl.glPopAttrib();
		}
		return true;
	}

}
